﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Mysoft.App.Appservices.Basic;
using Mysoft.Common.Const;
using Mysoft.Common.Extensions;
using Mysoft.Domain;
using Mysoft.Domain.Basic;
using Mysoft.Dto;
using System.Reflection;

namespace Mysoft.App.Appservices
{
    /// <summary>
    ///  测试
    /// </summary>

    public class HomeAppService : AppServiceBase
    {
        private readonly DomainService<UserDomainService> _domainService;
        private readonly DomainService<ErrorLogService> _logDomainService;


        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="domainService"></param>
        /// <param name="logDomainService"></param>
        public HomeAppService(DomainService<UserDomainService> domainService, DomainService<ErrorLogService> logDomainService)
        {
            _domainService = domainService;
            _logDomainService = logDomainService;

        }

      
     

        /// <summary>
        ///  反射
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public JsonResult test()
        {
         

            var t = new UserSignIn();
            var files = GetFields<UserSignIn>(t);
            files.ForEach(item =>
            {
                Console.WriteLine(item);
            });
            return Success("");
        }
        private List<string> GetFields<T>(T t)
        {
            List<string> ListStr = new List<string>();
            if (t == null)
            {
                return ListStr;
            }
            System.Reflection.FieldInfo[] fields = t.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            if (fields.Length <= 0)
            {
                return ListStr;
            }
            foreach (System.Reflection.FieldInfo item in fields)
            {
                string name = item.Name; //名称
                object value = item.GetValue(t);  //值

                if (item.FieldType.IsValueType || item.FieldType.Name.StartsWith("String"))
                {
                    ListStr.Add(name);
                }
                else
                {
                    GetFields(value);
                }
            }
            return ListStr;
        }

     

        /// <summary>
        /// 新增一个用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUser(UserDto dto)
        {
           
            Guid userId = Guid.Empty.NewSeqGuid();

            _domainService.Instance.addUser(dto);
            base._CacheContext.Remove("Users");
            return Success("提交成功");
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getUsers()
        {
            var _cacheUser = base._UserContext.UserInfo;
            var users = new List<UserDto>();
            //缓存
            if (base._CacheContext.Exists(CacheKeys.HomeAppService_getUsers))
            { 
                users = base._CacheContext.Get<List<UserDto>>(CacheKeys.HomeAppService_getUsers);
            }
            else {
                users = _domainService.Instance.getUsers().Result;
                base._CacheContext.AddObject(CacheKeys.HomeAppService_getUsers, users, 300);
            }
            return Success(users);
        }
        /// <summary>
        /// 获取所有日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getLogs()
        {
            var users = _logDomainService.Instance.GetAll();
            return Success(users);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult modifyUserName(string oldName,string newName)
        {
           _domainService.Instance.modifyUserName(oldName,newName);
            return Success("");
        }

        /// <summary>
        /// 获取并发计量数
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public JsonResult GetCacheStressTesting() 
        {
            var val = base._CacheContext.Get(CacheKeys.HomeAppService_StressTesting);
            return Success(val);
        }
        /// <summary>
        /// 清空并发计量数
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public JsonResult RemoveCacheStressTesting()
        {
            var val = base._CacheContext.Remove(CacheKeys.HomeAppService_StressTesting);
            return Success(val);
        }

        /// <summary>
        /// 测试并发
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet, HttpPost]
        public JsonResult StressTesting(bool islock) 
        {
            int val = 0;
            if (islock)
            {
                lock (lockobj)
                {
                    isLock(ref val);
                }
            }
            else 
            {
                isLock(ref val);
            }
            return Success(val);
        }
        private void isLock(ref int val) 
        {
            bool any = base._CacheContext.Exists(CacheKeys.HomeAppService_StressTesting);
            if (!any)
            {
                base._CacheContext.AddObject(CacheKeys.HomeAppService_StressTesting, 0);
            }
            val = base._CacheContext.Get(CacheKeys.HomeAppService_StressTesting).ToInt();
            val = val + 1;
            base._CacheContext.AddObject(CacheKeys.HomeAppService_StressTesting, val);
        }

        private static object  lockobj = new object();    


    }
}
